import sys
from PyQt5.QtWidgets import (
    QApplication, QMainWindow, QVBoxLayout, QWidget,
    QLabel, QComboBox, QPushButton, QFileDialog, QLineEdit
)
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QGuiApplication
import sys
import os

sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from utils import ArgumentParser, ConfigLoader, LOG
from model import GLMModel, OpenAIModel
from translator import PDFTranslator

class WorkerThread(QThread):
    progress_signal = pyqtSignal(str)  # 用于发送消息的信号

    def __init__(self, model, language, file_path, output_format):
        super().__init__()
        self.model = model
        self.language = language
        self.file_path = file_path
        self.output_format = output_format

    def run(self):
        # 模拟耗时任务
        file_name = os.path.basename(self.file_path)
        self.progress_signal.emit("任务开始...")
        self.progress_signal.emit(f"正在处理文件 {file_name} ...")
        # 实例化 PDFTranslator 类，并调用 translate_pdf() 方法
        translator = PDFTranslator(self.model)
        translator.translate_pdf(self.file_path, self.output_format,self.language)
        self.progress_signal.emit("任务完成！")



class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("模型选择与文件处理")
        self.setGeometry(100, 100, 500, 300)

        # 居中窗口
        self.center_window()
        
        # 主窗口布局
        layout = QVBoxLayout()

        # 1. 模型选择下拉框
        self.model_label = QLabel("选择模型:")
        layout.addWidget(self.model_label)

        self.model_combo = QComboBox()
        self.model_combo.addItems(["gpt-3.5-turbo", "gpt-4"])
        layout.addWidget(self.model_combo)

        # 2. 语言选择下拉框
        self.language_label = QLabel("选择语言:")
        layout.addWidget(self.language_label)

        self.language_combo = QComboBox()
        self.language_combo.addItems(["中文", "英文", "日文", "法文"])
        layout.addWidget(self.language_combo)

        # 3. PDF文件选择
        self.file_label = QLabel("选择PDF文件:")
        layout.addWidget(self.file_label)

        file_layout = QVBoxLayout()
        self.file_path_edit = QLineEdit()
        self.file_path_edit.setPlaceholderText("文件路径...")
        file_layout.addWidget(self.file_path_edit)

        self.file_button = QPushButton("浏览")
        self.file_button.clicked.connect(self.select_file)
        file_layout.addWidget(self.file_button)
        layout.addLayout(file_layout)

        # 4. 输出格式下拉框
        self.output_format_label = QLabel("选择输出格式:")
        layout.addWidget(self.output_format_label)

        self.output_format_combo = QComboBox()
        self.output_format_combo.addItems(["PDF", "Markdown"])
        layout.addWidget(self.output_format_combo)

        # 5. 执行动作按钮
        self.execute_button = QPushButton("翻译")
        self.execute_button.clicked.connect(self.execute_action)
        layout.addWidget(self.execute_button)

        # 创建按钮
        self.quit_button = QPushButton("退出程序", self)
        self.quit_button.clicked.connect(self.close_app)  # 按钮连接到关闭函数
        layout.addWidget(self.quit_button)


        # 进度显示
        self.status_label = QLabel("")
        layout.addWidget(self.status_label)

        # 设置中央小部件
        container = QWidget()
        container.setLayout(layout)
        self.setCentralWidget(container)

    def select_file(self):
        file_path, _ = QFileDialog.getOpenFileName(self, "选择PDF文件", "", "PDF Files (*.pdf)")
        if file_path:
            self.file_path_edit.setText(file_path)

    def execute_action(self):
        model_name = self.model_combo.currentText()
        language = self.language_combo.currentText()
        pdf_file_path = self.file_path_edit.text()
        file_format = self.output_format_combo.currentText()

        
        model = OpenAIModel(model=model_name, api_key='OPENAI_API_KEY')

        if not pdf_file_path:
            self.status_label.setText("请先选择文件！")
            return

        # 创建并启动后台线程
        self.worker = WorkerThread(model, language, pdf_file_path, file_format)
        self.worker.progress_signal.connect(self.update_status)  # 连接信号
        self.worker.start()

    def center_window(self):
        screen = QGuiApplication.primaryScreen().geometry()
        window = self.geometry()

        x = (screen.width() - window.width()) // 2
        y = (screen.height() - window.height()) // 2
        self.move(x, y)

    def update_status(self, message):
        self.status_label.setText(message)


    def close_app(self):
        # 关闭应用程序
        self.close()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())
